"""
basepage.py


"""
from time import sleep

from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

# 创建一个 basepage:   包括页面元素操作: input send_keys button click select
  # 创建一个浏览器,
class BasePage():
    def __init__(self, driver):
        self.driver = driver
    # 打开项目地址  ,并且设置隐式等待
    def open(self,url,timeout):
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(timeout)
    # 获取 元素定位方法
    def converter(self,target): # (By.ID,KW)  target = "id=kw" 'xpath="XXXXX"'
        by ,value =target.split('=', 1)
        return by.strip(), value.strip()  # strip() 去除by 和 value 左右两边的空格

    def locate_element(self, target):
        by,value = self.converter(target)

        ele = self.driver.find_element(by, value)
        return ele
    def input(self, target, value):
        self.locate_element(target).send_keys(value)

    def button(self, target):
        self.locate_element(target).click()

    def select_by_text(self, target, text):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_visible_text(text)

    def select_by_index(self, target,  index):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_index(index)

    def select_by_value(self, target, value):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_value(value)

    def wait(self,secend):
        sleep(secend)

    def switch_to_frame(self, name):
        self.driver.switch_to.iframe(name)


    def quit(self):
        self.driver.quit()

    def get_text(self, target):
        return self.locate_element(target).text